﻿Public Class frmLogin

    Private Sub frmLogin_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtUsuario.Text = "" Then
            ErrorProvider1.SetError(txtUsuario, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtUsuario, "")
        End If

        If txtPassword.Text = "" Then
            ErrorProvider1.SetError(txtPassword, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtPassword, "")
        End If
        '
        Return controlCampos

    End Function

    Dim controller As New Class_RC_Controller.rc_Mantenimiento

    Private Sub btnAceptar_Click(sender As System.Object, e As System.EventArgs) Handles btnAceptar.Click
        Try

            If Not ValidarCampos() Then

                Dim idUser As Integer = 0
                idUser = controller.Login(oiml.dbConn, txtUsuario.Text, txtPassword.Text)

                If idUser > 0 Then

                    Dim dt As New DataTable()
                    dt = controller.get_Usuario_All(oiml.dbConn, idUser, "", "", "", 0)

                    If Not dt Is Nothing Then
                        
                        oiml.idUsuario = idUser
                        oiml.Usuario = dt.Rows(0)("usuario")
                        oiml.Log(idUser, "INGRESO AL SISTEMA", 35)

                        Me.DialogResult = Windows.Forms.DialogResult.OK
                        Me.Close()

                    Else
                        
                        MessageBox.Show("Error: Favor verifique los datos del usuario.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        Me.txtUsuario.Focus()
                        Me.txtUsuario.Text = ""
                        Me.txtPassword.Text = ""
                    End If

                ElseIf idUser < 0 Then

                    MessageBox.Show("Error: Usuario inactivo.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error)

                Else
                    MessageBox.Show("Error: Usuario y/o contraseña incorrectos.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Me.txtUsuario.Focus()
                    Me.txtUsuario.Text = ""
                    Me.txtPassword.Text = ""
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Login", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click

        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()

    End Sub

    Private Sub txtPassword_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtPassword.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnAceptar_Click(sender, e)
        End If
    End Sub

    Private Sub txtUsuario_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtUsuario.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnAceptar_Click(sender, e)
        End If
    End Sub

End Class
